package com.example.mallsystem1104.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mallsystem1104.annotation.Authority;
import com.example.mallsystem1104.common.Result;
import com.example.mallsystem1104.constants.Constants;
import com.example.mallsystem1104.entity.AuthorityType;
import com.example.mallsystem1104.entity.LoginForm;
import com.example.mallsystem1104.entity.User;
import com.example.mallsystem1104.entity.dto.UserDTO;
import com.example.mallsystem1104.service.UserService;
import com.example.mallsystem1104.utils.TokenUtils;
import com.sun.xml.fastinfoset.stax.events.Util;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zy
 * @Date 2024/12/12 9:53
 * @packageName:com.example.mallsystem1104.controller
 * @ClassName:UserController
 */
@RestController
@RequestMapping
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody LoginForm loginForm){
        UserDTO userDTO=userService.login(loginForm);
        return Result.success(userDTO);
    }

    @PostMapping("/register")
    public Result register(@RequestBody LoginForm loginForm){
        User user=userService.register(loginForm);
        return Result.success(user);
    }

    @GetMapping("/userid")
    public long getUserId(){
        return TokenUtils.getCurrentUser().getId();
    }


    /**
     * 根据用户名获取用户信息
     *
     * @param username 用户名，通过URL路径参数传递
     * @return 返回包含用户信息的Result对象
     */
    @GetMapping("/userinfo/{username}")
    public Result getUserInfoByName(@PathVariable String username){
        // 调用userService的getOneUser方法获取用户信息
        User one=userService.getOneUser(username);
        // 返回成功结果，包含用户信息
        return Result.success(one);
    }

    @PostMapping("/user")
    public Result save(@RequestBody User user){
        return Result.success(userService.saveOrUpdate(user));
    }

    /**
     * 根据分页参数和查询条件获取用户列表
     *
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @param id 用户ID，用于模糊查询
     * @param username 用户名，用于模糊查询
     * @param nickname 昵称，用于模糊查询
     * @return 返回分页查询结果
     */
    @GetMapping("/user/page")
    public Result findPage(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            String id,
            String username,
            String nickname
    ){
        // 创建用户分页对象
        IPage<User> userPage=new Page<>(pageNum,pageSize);
        // 创建查询条件包装器
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();

        // 如果ID非空，添加ID模糊查询条件
        if (!Util.isEmptyString(id)){
            queryWrapper.like("id",id);
        }
        // 如果用户名非空，添加用户名模糊查询条件
        if (!Util.isEmptyString(username)){
            queryWrapper.like("username",username);
        }
        // 如果昵称非空，添加昵称模糊查询条件
        if (!Util.isEmptyString(nickname)){
            queryWrapper.like("nickname",nickname);
        }
        // 添加按ID降序排序条件
        queryWrapper.orderByDesc("id");

        // 打印当前用户信息（调试用）
        System.out.println("======"+TokenUtils.getCurrentUser());

        // 执行分页查询，并返回成功结果
        return Result.success(userService.page(userPage,queryWrapper));
    }

    @GetMapping("/user/resetPassword")
    public Result resetPassword(@RequestParam String id,@RequestParam String newPassword){
        userService.resetPassword(id,newPassword);
        return Result.success();

    }

    @Authority(AuthorityType.requireAuthority)
    @DeleteMapping("/user/{id}")
    public Result deleteById(@PathVariable int id){
        boolean isdelete=userService.removeById(id);
        if (isdelete){
            return Result.success();
        }else {
            return Result.error(Constants.CODE_500,"删除失败");
        }
    }

    @Authority(AuthorityType.requireAuthority)
    @PostMapping("/user/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        boolean isdelete=userService.removeBatchByIds(ids);
        if (isdelete){
            return Result.success();
        }else {
            return Result.error(Constants.CODE_500,"删除失败");
        }
    }




}
